package com.tikou.ui.view.fallingview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tikou on 2017/12/25 09:56.
 * 作者：wangwei
 * 邮箱：ww@winning.com.cn
 * 说明：下雪的特效
 * 参考：https://juejin.im/post/5a32b34c51882506146ef1a0
 */

public class FallingView extends View{
    private List<FallObject> fallObjects;
    private Context mContext;
    private AttributeSet mAttrs;
    private int viewWidth;
    private int viewHeight;
    private static final int defaultWidth = 600;//默认宽度
    private static final int defaultHeight = 1000;//默认高度
    private static final int intervalTime = 5;//重绘间隔时间
    private Paint mPaint;
    private int snowY;
    public FallingView(Context context) {
        this(context,null);
    }

    public FallingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FallingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        snowY = 0;
        fallObjects=new ArrayList<>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = measureSize(defaultWidth,widthMeasureSpec);
        int height = measureSize(defaultHeight,heightMeasureSpec);
        setMeasuredDimension(width,height);
        viewWidth = width;
        viewHeight = height;
    }

    private int measureSize(int defaultSize, int measureSpec) {
        int result = defaultSize;
        int specMode = View.MeasureSpec.getMode(measureSpec);
        int specSize = View.MeasureSpec.getSize(measureSpec);
        Log.e("===","specSize="+specSize);
        Log.e("===","defaultSize="+defaultSize);
        if (specMode == MeasureSpec.EXACTLY){
            result = specSize;
        }else if (specMode == MeasureSpec.AT_MOST){
            result = Math.min(result,specSize);
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawCircle(100,snowY,25,mPaint);
        if (fallObjects.size()>0){
            for (int i=0;i<fallObjects.size();i++){
                fallObjects.get(i).drawObject(canvas,mPaint);
                getHandler().postDelayed(runnable,intervalTime);
            }
        }
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

    public void addFallObject(final FallObject fallObject, final int num){
        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                getViewTreeObserver().removeOnPreDrawListener(this);
                for (int i=0;i<num;i++){
                    FallObject fallObject1 = new FallObject(fallObject.builder,viewWidth,viewHeight);
                    fallObjects.add(fallObject1);
                }
                invalidate();
                return true;
            }
        });
    }
}
